package com.akl.ssmspringboot.controller.poi;

import com.akl.ssmspringboot.entity.User;
import com.akl.ssmspringboot.service.UserService;
import com.akl.ssmspringboot.utils.ExcelUtil;
import com.akl.ssmspringboot.utils.ExportExcel;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @ClassName:
 * @Author: Guoxy
 * @Date: Created in 15:42 2018/12/3
 * @Version:1.0
 */
@Controller
@RequestMapping(value = "/export")
public class ExportExcelController {

    @Autowired
    private UserService userService;

    @RequestMapping(value = "/export2")
    @ResponseBody
    public void export2(HttpServletResponse response) {
        //获取数据
        List<User> userList = userService.findAll();

        SimpleDateFormat sdf =  new SimpleDateFormat("yyyyMMdd");
        String time = sdf.format(new Date());
        String fileName = "Excel名字" + time + ".xls";

        String title = "表格标题";

        String[] rowsName = new String[]{"序号","姓名","年龄","班级","学号","录入时间"};

        List<Object[]> dataList = new ArrayList<Object[]>();
        Object[] objs = null;
        for (int i = 0; i < userList.size(); i++) {
            User user = userList.get(i);
            objs = new Object[rowsName.length];
            objs[0] = i;
            objs[1] = user.getUsername();
            objs[2] = user.getYear();
            objs[3] = user.getClass();
            objs[4] = user.getEmail();
            SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss SSS");
            String date = df.format(new Date());
            objs[5] = date;
            dataList.add(objs);
        }
        ExportExcel ex = new ExportExcel(fileName, title, rowsName, dataList);
        ex.export(response);
        int i = 1;
    }

    /**
     * 导出表格
     *
     * @return
     */
    @RequestMapping(value = "/export")
    @ResponseBody
    public void export(HttpServletResponse response) {
        //获取数据
        List<User> list = userService.findAll();
        //excel标题
        String[] title = {"姓名", "性别", "年龄", "地区", "电话", "邮箱"};
        Date date = new Date();
        SimpleDateFormat sdf =  new SimpleDateFormat("yyyyMMdd");
        String time = sdf.format(date);
        //excel文件名
        String fileName = "用户信息表" + time + ".xls";
        //sheet名
        String sheetName = "用户信息表";
        String[][] content = new String[list.size()][title.length];
        for (int i = 0; i < list.size(); i++) {
            User user = list.get(i);
            content[i][0] = user.getUsername();
            if(user.getGender()!=null && user.getGender().equals("0")){
                content[i][1] = "女";
            }else if(user.getGender()!=null && user.getGender().equals("1") ){
                content[i][1] = "男";
            }else{
                content[i][1] = "未知";
            }
            content[i][2] = user.getYear();
            content[i][3] = user.getAreaName();
            content[i][4] = user.getPhone();
            content[i][5] = user.getEmail();
        }
        //创建HSSFWorkbook
        HSSFWorkbook wb = ExcelUtil.getHSSFWorkbook(sheetName, title, content, null);
        //响应到客户端
        try {
            this.setResponseHeader(response, fileName);
            OutputStream os = response.getOutputStream();
            wb.write(os);
            os.flush();
            os.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 发送响应流方法
     * @param response
     * @param fileName
     */
    public void setResponseHeader(HttpServletResponse response, String fileName) {
        try {
            try {
                fileName = new String(fileName.getBytes(),"ISO8859-1");
            } catch (UnsupportedEncodingException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            response.setContentType("application/octet-stream;charset=ISO8859-1");
            response.setHeader("Content-Disposition", "attachment;filename="+ fileName);
            response.addHeader("Pargam", "no-cache");
            response.addHeader("Cache-Control", "no-cache");
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

}